<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
// 应用公共文件
//共用返回json函数
function xjson($code=0,$msg='操作成功',$data=[]){
    $json=['code'=>0,'msg'=>$msg,'data'=>$data];
    $num= func_num_args();
    for ($i=0;$i<$num;$i++){
        $value=func_get_arg($i);
        if(is_numeric($value)){
            $json['code']=$value;
            if($value){
                $json['msg']='操作失败！';
            }else{
                $json['msg']='操作成功！';
            }
        }elseif(is_string($value)){
            $json['msg']=$value;
        }else{
            $json['data']=$value;
        }
    }
    return \think\Response::create($json, 'json',200);
}
/**
 * 无限级分类，不带层级
 * @param $data '所有分类数据
 * @param int $pid
 * @param string $title 给数据中哪个字段加上-“---”分隔符，区别层级
 * @param int $level
 * @param string $field
 * @param Closure|null $f
 * @return array
 */
function unlimitedSort($data,$pid = 0, $level=1,$field = 'pid',$title='title',Closure $f=null)
{
    if(!empty($data)&&$data[0] instanceof \think\Model ){
        $data= $data->toArray();
    }
    global $tree;
    foreach ($data as $item) {
        if ($item[$field] == $pid) {
            $item['level'] = $level;
            $item['pid']=(int)$item['pid'];
            if(isset($f)){
                $f($item);
            }
            $item['_title']=str_repeat('　　',$level-1).'├'.$item[$title];
            $tree[] = $item;
            unlimitedSort($data,$item['id'],$level+1,$field,$title,$f);
        }
    }
    return $tree;
}


/**
 * 返回带children层级的无限级分类
 * @param $data
 * @param int $pid
 * @param int $level
 * @param string $field
 * @param string $childNode
 * @param Closure|null $f
 * @return array
 */
function unlimitedTree($data,$pid = 0, $level=1,$field = 'pid', $childNode = 'children',Closure $f=null)
{
    if(!empty($data)&&$data[0] instanceof \think\Model ){
        $data= $data->toArray();
    }
    $tree = [];
    foreach ($data as $item) {
        if ($item[$field] == $pid) {
            $item[$childNode] = unlimitedTree($data,$item['id'],$level+1,$field,$childNode,$f);
            $item['level'] = $level;
            $item['pid']=(int)$item['pid'];
            if(isset($f)){
                $f($item);
            }
            $tree[] = $item;
        }
    }
    return $tree;
}

/**
 * 获得所有父级栏目
 * @param $data 栏目数据
 * @param $id 子栏目
 * @param string $fieldPri 唯一键名，如果是表则是表的主键
 * @param string $fieldPid 父ID键名
 * @return array
 */
function parentNode($data, $id, $fieldPri = 'id', $fieldPid = 'pid')
{
    if(!empty($data)&&$data[0] instanceof \think\Model ){
        $data= $data->toArray();
    }
    $arr = array();
    foreach ($data as $v) {
        if ($v[$fieldPri] == $id) {
            $arr[] = $v;
            $_n =parentNode($data, $v[$fieldPid], $fieldPri, $fieldPid);
            if (!empty($_n)) {
                $arr = array_merge($arr, $_n);
            }
        }
    }
    return $arr;
}
